local fuqing = fk.CreateSkill{
  name = "xiaobai__fuqing",
}

Fk:loadTranslationTable{
  ["xiaobai__fuqing"] = "扶倾",
  [":xiaobai__fuqing"] = "每轮限一次，一张【杀】指定目标后，若途经你或目标为你，你可以令之无效，然后视为使用一张【以逸待劳】，"..
    "然后此【杀】的使用者可使用至多X张因此弃置的牌（X为【以逸待劳】的目标数）。",
  
  ["#xiaobai__fuqing_prompt"] = "扶倾：你可以令此【杀】无效并视为使用【以逸待劳】然后 %dest 可使用至多X张因此弃置的牌（X为【以逸待劳】的目标数）",
  ["#xiaobai__fuqing_extraUsePrompt"] = "扶倾：你可以视为其中至多 %arg 张牌" 
}

---从from到target是否经过pass
---@param from ServerPlayer
---@param target ServerPlayer
---@param pass ServerPlayer
---@return boolean
local passPlayer = function (from, target, pass)
  if target == from then return false end
  local p = from:getNextAlive()
  local num1,num2 = 1,1
  local judge1,judge2 = false,false
  while p ~= from do
    if p == pass then 
      judge1 = true 
    end
    if p == target then 
      break 
    end
    p = p:getNextAlive()
    num1 = num1 + 1
  end
  p = from:getLastAlive()
  while p ~= from do
    if p == pass then 
      judge2 = true
    end
    if p == target then 
      break 
    end
    p = p:getLastAlive()
    num2 = num2 + 1
  end
  if num1 < num2 then
    return judge1
  elseif num1 > num2 then
    return judge2
  else
    return judge1 or judge2
  end
end
fuqing:addEffect(fk.TargetSpecified, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if not (player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryRound) < 1 and data.firstTarget and data.card.trueName == "slash") then return end
    local targets = data:getAllTargets()
    if table.contains(targets, player) then return true end
    for _, tid in ipairs(targets) do
      if passPlayer(target, tid, player) then
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(player,{
      name = "await_exhausted", 
      skill_name = self.name,
      prompt = "#xiaobai__fuqing_prompt::"..target.id,
      skip = true})
    if use then
      event:setCostData(self, use)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "xiaobai__fuqing_using")
    local cardTargets = #Fk:cloneCard("await_exhausted"):getAvailableTargets(player)
    room:useCard(
      event:getCostData(self)
    )
    local cards = player:getTableMark("xiaobai__fuqing_discards")
    for i = 1, cardTargets, 1 do
      local use = room:askToUseRealCard(target,{
        pattern = cards,
        skill_name = self.name,
        prompt = "#xiaobai__fuqing_extraUsePrompt:::"..(cardTargets - i + 1),
        extra_data = {bypass_times = true, expand_pile = cards}})
      if not use then break end
      table.removeOne(cards, use.card.id)
      if target.dead then return end
      if #cards == 0 then break end
    end
    room:setPlayerMark(player, "xiaobai__fuqing_discards", 0)
    room:setPlayerMark(player, "xiaobai__fuqing_using", 0)
    table.insertTableIfNeed(data.use.nullifiedTargets, room:getAlivePlayers())
    return true
  end,  
})

fuqing:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self.name) and player:getMark("xiaobai__fuqing_using") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.moveReason == fk.ReasonDiscard and move.skillName and move.skillName and move.skillName == "await_exhausted_skill" then
        for _, info in ipairs(move.moveInfo) do
          player.room:addTableMark(player, "xiaobai__fuqing_discards", info.cardId)
        end
      end
    end
  end
})

return fuqing
